package org.ling.xuan.util;


import cn.hutool.core.util.StrUtil;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.project.Project;

import java.util.HashMap;
import java.util.Map;


public class LoggerUtil {
    private static final Map<String, ConsoleView> consoleViews = new HashMap<>();
    public static void log(Project project, String message) {
        ConsoleView consoleView = getConsoleView(project);
        if (consoleView != null) {
            consoleView.print(message + "\n", ConsoleViewContentType.NORMAL_OUTPUT);
        }
    }


    private static ConsoleView getConsoleView(Project project) {
        if (consoleViews.containsKey(LxStrUtil.unionPath(project.getBasePath()))) {
            ConsoleView consoleView= consoleViews.get(LxStrUtil.unionPath(project.getBasePath()));
            return consoleView;
        } else {
            return null;
        }
    }

    public static void add(ConsoleView consoleView, Project project) {
        FileLogUtil.log("添加底部工具栏输出框到缓存:{}",project.getBasePath());
        consoleViews.put(LxStrUtil.unionPath(project.getBasePath()), consoleView);
    }



    public static void log(Project project, String message, Object... objects) {
        String msg = message;
        if (objects != null) {
            Object[] os = new Object[objects.length];
            for (int i = 0; i < objects.length; i++) {
                if (objects[i] instanceof Exception) {
                    os[i] = ExUtil.getExMsg((Exception) objects[i], "\n");
                } else {
                    os[i] = objects[i];
                }
            }
            msg = StrUtil.format(message, os);
        }
        log(project, msg);
    }
    
}
